﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LS.Common;
using LS.Entitys;
using LS.Cores;
using Dapper;

namespace LS.Cores
{
    /// <summary>
    /// 提供系统配置服务
    /// </summary>

    public class SysConfigurationService : BaseService<SysConfigurationEntity>, ISysConfiguration
    {
        #region 实现接口方法

        /// <summary>
        /// 根据编码获得配置信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public SysConfigurationEntity GetByCode(string code)
        {
            string sql = "select * from  sys_configure where code=@code";
            return SqlHelper.Query<SysConfigurationEntity>(sql, new { code = code }).FirstOrDefault();
        }
        /// <summary>
        /// 根据编码获得配置信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetValueByCode(string code)
        {
            SysConfigurationEntity config = GetByCode(code);
            if (config != null)
            {
                return config.Args;
            }
            return "";
        }
      
        #endregion 实现接口方法

        /// <summary>
        /// 查询编码是否重复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="bm"></param>
        /// <returns></returns>
        public bool ExistsBm(string id, string bm)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.Append("select count(1)  From  sys_configure where 1=1 ");
            if (!string.IsNullOrEmpty(id))
            {
                strsql.Append(" and  id!='" + id + "'");
            }
            if (!string.IsNullOrEmpty(bm))
            {
                strsql.Append(" and  code='" + bm + "'");
            }
            return SqlHelper.ExecuteScalar<int>(strsql.ToString()) > 0 ? true : false;
        }

        
        public string GetBillNumber(string billName, string prefixes, int isContinuous = 0, int billLen = 6)
        {
            var p = new DynamicParameters();
            p.Add("BillName", billName);
            p.Add("Prefixes", prefixes);
            p.Add("IsContinuous", isContinuous);
            p.Add("BillLen", billLen);
            p.Add("OutPutBillNo","", dbType: DbType.String, direction: ParameterDirection.Output,200);
            SqlHelper.Execute("p_getbillNo", p, commandType: CommandType.StoredProcedure);
            return p.Get<string>("OutPutBillNo");
        }
    }
}
